package api

import (
	"fmt"
	"mytest/initialize/database"
	"mytest/models"
	"mytest/models/request"
	"mytest/service/system"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Users(c *gin.Context) {
	var req request.UserList
	if e := c.Bind(&req); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	db := database.GetDB()
	e, users, total := system.GetUserList(db, req)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("获取用户失败: %v", e.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: gin.H{
			"total": total,
			"data":  users,
		},
	})
}
func SetUserAuthority(c *gin.Context) {
	var req models.SysUser
	if e := c.Bind(&req); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if req.AuthorityId == "" {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  "参数AuthorityId为空",
		})
		return
	}
	err := system.SetUserAuth(database.GetDB(), req)
	if err != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("设置用户失败: %v", err.Error()),
		})
		return
	}
	c.JSON(http.StatusOK, UniversalResp{
		Msg: "success",
	})
}
